package com.service.impl;

import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.pojo.Page;
import com.pojo.Tour;
import com.pojo.User;
import com.service.UserService;

import java.util.List;

/**
 * @Author hjb
 * @Date 7/6/2021 下午 10:13
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class UserServiceImpl implements UserService {

    private UserDaoImpl uDI = new UserDaoImpl();

    @Override
    public int deleteUserById(Integer id) {
        return uDI.deleteUser(id);
    }

    @Override
    public void regist(User user) {
        uDI.saveUser(user);
    }

    @Override
    public User login(User user) {
        return uDI.queryUserByNameAndPassword(user.getUsername(),user.getPassword());
    }

    @Override
    public boolean existsUserName(String username) {
        if(uDI.queryUserByUserName(username) != null){
            return true;
        }
        return false;
    }

    @Override
    public Page<User> page(int pageNo, int pageSize) {
        Page<User> page = new Page<>();

        //设置当前页码
        page.setPageNo(pageNo);

        //设置每个页面显示数量
        page.setPageSize(pageSize);

        //设置总记录数
        Integer pageTotalCount = uDI.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);

        //设置总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize > 0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);

        //设置当前页数据
        int beginNo = (page.getPageNo() - 1) * pageSize;
        List<User> items = uDI.queryForPageItems(beginNo,pageSize);
        page.setItems(items);

        return page;
    }

    @Override
    public User queryUserById(Integer id) {
        return uDI.queryUserById(id);
    }

    @Override
    public int updateUser(User user) {
        return uDI.updateUser(user);
    }
}
